

#include <material/materialmenubutton.h>
#include <qevent.h>
#include <qmenu.h>
#include <qaction.h>

cceMaterialMenuButton::cceMaterialMenuButton(QWidget *parent)
  : QLabel(parent)
  , _active (false)
{
  _menu = new QMenu(this);
  _actions[Action_Clear]                = _menu->addAction("Clear");
  _actions[Action_UpdateGraphicsSystem] = _menu->addAction("Update graphics system");
  _menu->addSeparator();
  _actions[Action_Save]                 = _menu->addAction("Save");
  _actions[Action_SaveAll]              = _menu->addAction("Save all");
  _actions[Action_Export]               = _menu->addAction("Export");
  _menu->addSeparator();
  _actions[Action_Hide]                 = _menu->addAction("Hide");

  _actions[Action_Save]->setShortcut(QKeySequence (Qt::CTRL + Qt::Key_S));
  _actions[Action_Hide]->setShortcut(QKeySequence (Qt::CTRL + Qt::Key_M));

  connect (_actions[Action_Clear], SIGNAL(triggered()), this, SIGNAL(Clear()));
  connect (_actions[Action_UpdateGraphicsSystem], SIGNAL(triggered()), this, SIGNAL(UpdateGraphicsSystem()));
  connect (_actions[Action_Save], SIGNAL(triggered()), this, SIGNAL(Save()));
  connect (_actions[Action_SaveAll], SIGNAL(triggered()), this, SIGNAL(SaveAll()));
  connect (_actions[Action_Export], SIGNAL(triggered()), this, SIGNAL(Export()));
  connect (_actions[Action_Hide], SIGNAL(triggered()), this, SIGNAL(Hide()));
}


void cceMaterialMenuButton::mousePressEvent(QMouseEvent *ev)
{
  grabMouse();
  _active = true;
}

void cceMaterialMenuButton::mouseReleaseEvent(QMouseEvent *ev)
{
  releaseMouse();
  if (rect().contains(ev->pos()) && _active)
    {
      _active = false;

      _menu->popup(mapToGlobal(QPoint (0, height ())));
    }
}


QAction *cceMaterialMenuButton::GetAction(Action action)
{
  return _actions[action];
}
